﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Const;
using Entitas;
using UnityEngine;

public class EliminateExplodeSystem: ReactiveSystem<GameEntity>
{
    private Contexts _contexts;
    public EliminateExplodeSystem(Contexts context) : base(context.game)
    {
        _contexts = context;
    }

   

    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
    {
        return context.CreateCollector(GameMatcher.ComponentsDestroy);
    }

    protected override bool Filter(GameEntity entity)
    {
        return entity.hasComponentsItemEffectState
               && entity.componentsItemEffectState.EffectState == ItemEffectName.EXPLODE;
    }

    protected override void Execute(List<GameEntity> entities)
    {
        foreach (GameEntity entity in  entities)
        {
            customData pos = entity.componentsItemIndex.index;
            GameEntity[] temp;
            for (int x=pos.x-1;x<=pos.x+1;x++)
            {
                for (int y = pos.y-1; y <= pos.y+1; y++)
                {
                    if (x < 0 || y < 0)
                    {
                        continue;
                    }

                    temp = _contexts.game.GetEntitiesWithComponentsItemIndex(new customData(x, y)).ToArray();
                    if (temp.Length==1)
                    {
                        temp[0].isComponentsDestroy = true;
                    }
                    
                }
            }

        }
    }
}
